﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using System.Text;
using System.Collections.Generic;

public partial class BaseData_SendEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400113", "00033");
        if (!IsPostBack)
        {
            List<Model.V_CARSET> vl = new BLL.V_CARSET().GetAllList();
            DropDownList1.DataSource = vl;
            DropDownList1.DataTextField = "MC";
            DropDownList1.DataValueField = "DM";
            DropDownList1.DataBind();
            ListItem li = new ListItem("", "-1");
            DropDownList1.Items.Insert(0, li);

        }
        Session["FCode"] = "300400113";
    }
    private void BindDataList()
    {
        string sqlstr = "";

        string FCode = Session["FCode"].ToString();
        if (!Check.CheckPermission(FCode, "00033"))
        {


            if (txtDM.Text.Trim() == "" && txtName.Text.Trim() == "" && txtNickName.Text.Trim() == "" && DropDownList1.SelectedValue == "-1" && txtCardCode.Text.Trim() == "" && txtDH.Text.Trim() == "" && txtPhone.Text.Trim() == "" && txtDZ.Text.Trim() == ""
                && txtQQ.Text.Trim() == "" && txtMSN.Text.Trim() == "" && txtWW.Text.Trim() == "" && txtBZ.Text.Trim() == "" && txtEmail.Text.Trim() == "")
            {
                sqlstr += " and V_Customer.DM='' ";
                sqlstr += " and V_CUSTOMER.GKMC='' ";
                sqlstr += " and V_CUSTOMER.BYZD5='' ";
                sqlstr += " and V_VIPSET.KLDM='' ";
                sqlstr += " and V_VIPSET.DM='' ";
                sqlstr += " and V_CUSTOMER.DH='' ";
                sqlstr += " and V_CUSTOMER.SJ='' ";
                sqlstr += " and V_CUSTOMER.DZ='' ";
                sqlstr += " and V_Customer_Auto.BYZD16='' ";//MSN
                sqlstr += " and V_Customer_Auto.BYZD17='' ";//旺旺
                sqlstr += " and V_Customer_Auto.BYZD18='' ";//QQ
                sqlstr += " and V_CUSTOMER.BZ='' ";//备注
                sqlstr += " and V_CUSTOMER.EMAIL='' ";//邮箱
            }
            else
            {
                sqlstr += txtDM.Text.Trim() == "" ? "" : " and V_Customer.DM='" + txtDM.Text.Trim() + "' ";
                sqlstr += txtName.Text.Trim() == "" ? "" : " and V_CUSTOMER.GKMC='" + txtName.Text.Trim() + "' ";
                sqlstr += txtNickName.Text.Trim() == "" ? "" : " and V_CUSTOMER.BYZD5='" + txtNickName.Text.Trim() + "' ";
                sqlstr += DropDownList1.SelectedValue == "-1" ? "" : " and V_VIPSET.KLDM='" + DropDownList1.SelectedValue + "' ";
                sqlstr += txtCardCode.Text.Trim() == "" ? "" : " and V_VIPSET.DM='" + txtCardCode.Text.Trim() + "' ";
                sqlstr += txtDH.Text.Trim() == "" ? "" : " and V_CUSTOMER.DH='" + txtDH.Text.Trim() + "' ";
                sqlstr += txtPhone.Text.Trim() == "" ? "" : " and V_Customer.SJ='" + txtPhone.Text.Trim() + "' ";
                sqlstr += txtDZ.Text.Trim() == "" ? "" : " and V_Customer.DZ='" + txtDZ.Text.Trim() + "' ";
                sqlstr += txtQQ.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD18='" + txtQQ.Text.Trim() + "' ";
                sqlstr += txtMSN.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD16='" + txtMSN.Text.Trim() + "' ";
                sqlstr += txtWW.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD17='" + txtWW.Text.Trim() + "' ";
                sqlstr += txtBZ.Text.Trim() == "" ? "" : " and V_Customer.BZ='" + txtBZ.Text.Trim() + "' ";
                sqlstr += txtEmail.Text.Trim() == "" ? "" : " and V_Customer.EMAIL='" + txtEmail.Text.Trim() + "' ";
            }



        }
        else
        {
            sqlstr += txtDM.Text.Trim() == "" ? "" : " and V_Customer.DM LIKE '%" + txtDM.Text.Trim() + "%' ";
            sqlstr += txtName.Text.Trim() == "" ? "" : " and V_CUSTOMER.GKMC LIKE '%" + txtName.Text.Trim() + "%' ";
            sqlstr += txtNickName.Text.Trim() == "" ? "" : " and V_CUSTOMER.BYZD5 LIKE '%" + txtNickName.Text.Trim() + "%' ";
            sqlstr += DropDownList1.SelectedValue == "-1" ? "" : " and V_VIPSET.KLDM LIKE '%" + DropDownList1.SelectedValue + "%' ";
            sqlstr += txtCardCode.Text.Trim() == "" ? "" : " and V_VIPSET.DM LIKE '%" + txtCardCode.Text.Trim() + "%' ";
            sqlstr += txtDH.Text.Trim() == "" ? "" : " and V_CUSTOMER.DH LIKE '%" + txtDH.Text.Trim() + "%' ";
            sqlstr += txtPhone.Text.Trim() == "" ? "" : " and V_Customer.SJ LIKE '%" + txtPhone.Text.Trim() + "%' ";
            sqlstr += txtDZ.Text.Trim() == "" ? "" : " and V_Customer.DZ LIKE '%" + txtDZ.Text.Trim() + "%' ";
            sqlstr += txtQQ.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD18 LIKE '%" + txtQQ.Text.Trim() + "%' ";
            sqlstr += txtMSN.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD16 LIKE '%" + txtMSN.Text.Trim() + "%' ";
            sqlstr += txtWW.Text.Trim() == "" ? "" : " and V_Customer_Auto.BYZD17 LIKE '%" + txtWW.Text.Trim() + "%' ";
            sqlstr += txtBZ.Text.Trim() == "" ? "" : " and V_Customer.BZ LIKE '%" + txtBZ.Text.Trim() + "%' ";
            sqlstr += txtEmail.Text.Trim() == "" ? "" : " and V_Customer.EMAIL LIKE '%" + txtEmail.Text.Trim() + "%' ";

        }

        List<Model.V_CUSTOMER> list = new BLL.V_CUSTOMER().GetEmailUserList(sqlstr);
        UserEmailListControl1.Bind(list);
    }
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400113", "00033"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        ArrayList al = new ArrayList();
        if (toEmail.Text.Trim().Length > 0)
        {
            string[] mails = toEmail.Text.Trim().Split(';');
            foreach (string str in mails)
            {
                al.Add(str);
            }
        }
        foreach (string str in UserEmailListControl1.Selected)
        {
            al.Add(str);
        }
        if (al.Count > 0)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object obj in al)
            {
                EmailSendBL emailBL = new EmailSendBL(obj.ToString(), "CustomSubject", body1.Text.Trim());
                string ret = emailBL.SendCustomEmail();
                if (!ret.Equals(def.SUCCESS))
                {
                    sb.Append(obj.ToString());
                    sb.Append(",");
                }
            }
            if (sb.Length > 0)
            { Share.Alert(this.Page, string.Format("{0}等发送邮件失败", sb.ToString().Substring(0, sb.ToString().Length - 1))); return; }

            Share.Alert(this.Page, "发送成功"); return;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataList();
    }
}
